home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / MOUSE.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  5KB  |  221 lines

  1. // ------------- mouse.cpp
  2.  
  3. #include <stdio.h>
  4. #include "desktop.h"
  5.  
  6. // -------- mouse constructor
  7. Mouse::Mouse()
  8. {
  9.     // ------- see if mouse driver is installed
  10.     unsigned char far *ms;
  11.     ms = (unsigned char far *)
  12.         MK_FP(peek(0, MOUSE*4+2), peek(0, MOUSE*4));
  13.     // --- if the interrupt vector is null or points to a retf,
  14.     //     the mouse driver is not installed
  15.     installed = (Bool) (ms != NULL && *ms != 0xcf);
  16.  
  17.     if (installed)    {
  18.         // --- get the mouse state buffer size
  19.         CallMouse(BUFFSIZE);
  20.         statebuffer = new char[regs.x.bx];
  21.         // --- save the mouse state
  22.         CallMouse(SAVESTATE, 0, 0,
  23.             FP_OFF(statebuffer), FP_SEG(statebuffer));
  24.         // --- reset the mouse
  25.         CallMouse(RESETMOUSE);
  26.         prevx = prevy =
  27.         clickx = clicky =
  28.         releasex = releasey -1;
  29.         SetTravel(0, desktop.screen().Width()-1, 0,
  30.                                 desktop.screen().Height()-1);
  31.     }
  32. }
  33.  
  34. Mouse::~Mouse()
  35. {
  36.     if (installed)    {
  37.         Hide();
  38.         // --- restore the mouse state
  39.         CallMouse(RESTORESTATE, 0, 0,
  40.             FP_OFF(statebuffer), FP_SEG(statebuffer));
  41.         delete [] statebuffer;
  42.     }
  43. }
  44.  
  45. void Mouse::GetPosition(int &mx, int &my)
  46. {
  47.     mx = my = 0;
  48.     if (installed)    {
  49.         CallMouse(READMOUSE);
  50.         mx = regs.x.cx/8;
  51.         my = regs.x.dx/8;
  52.         if (desktop.screen().Width() == 40)
  53.             mx /= 2;
  54.     }
  55. }
  56.  
  57. void Mouse::SetPosition(int x, int y)
  58. {
  59.     if (installed)    {
  60.         if (desktop.screen().Width() == 40)
  61.             x *= 2;
  62.         CallMouse(SETPOSITION,0,x*8,y*8);
  63.     }
  64. }
  65.  
  66. Bool Mouse::Moved()
  67. {
  68.     int x, y;
  69.     Bool rtn = False;
  70.     if (installed)    {
  71.         GetPosition(x, y);
  72.         rtn = (Bool) (x != prevx || y != prevy);
  73.         prevx = x;
  74.         prevy = y;
  75.     }
  76.     return rtn;
  77. }
  78.  
  79. void Mouse::Show()
  80. {
  81.     if (installed)
  82.         CallMouse(SHOWMOUSE);
  83. }
  84.  
  85. void Mouse::Hide()
  86. {
  87.     if (installed)
  88.         CallMouse(HIDEMOUSE);
  89. }
  90.  
  91. Bool Mouse::LeftButton()
  92. {
  93.     Bool rtn = False;
  94.     if (installed)    {
  95.         CallMouse(READMOUSE);
  96.         rtn = (Bool) ((regs.x.bx & 1) == 1);
  97.     }
  98.     return rtn;
  99. }
  100.  
  101. Bool Mouse::ButtonReleased()
  102. {
  103.     Bool rtn = False;
  104.     if (installed)    {
  105.         CallMouse(BUTTONRELEASED);
  106.         rtn = (Bool) (regs.x.bx != 0);
  107.     }
  108.     return rtn;
  109. }
  110.  
  111. void Mouse::SetTravel(int minx, int maxx, int miny, int maxy)
  112. {
  113.     if (installed)    {
  114.         if (desktop.screen().Width() == 40)    {
  115.             minx *= 2;
  116.             maxx *= 2;
  117.         }
  118.         CallMouse(XLIMIT, 0, minx*8, maxx*8);
  119.         CallMouse(YLIMIT, 0, miny*8, maxy*8);
  120.     }
  121. }
  122.  
  123. void Mouse::CallMouse(int m1,int m2,int m3,int m4, unsigned es)
  124. {
  125.     struct SREGS sregs;
  126.     segread(&sregs);
  127.     if (es != 0)
  128.         sregs.es = es;
  129.     regs.x.dx = m4;
  130.     regs.x.cx = m3;
  131.     regs.x.bx = m2;
  132.     regs.x.ax = m1;
  133.     int86x(MOUSE, ®s, ®s, &sregs);
  134. }
  135.  
  136. // ------ get the window to send mouse events
  137. DFWindow *Mouse::MouseWindow(int mx, int my)
  138. {
  139.     DFWindow *Mwnd;
  140.     if (desktop.FocusCapture() != NULL)
  141.         Mwnd = desktop.FocusCapture();
  142.     else
  143.         Mwnd = inWindow(mx, my);
  144.     return Mwnd;
  145. }
  146.  
  147. void Mouse::DispatchRelease()
  148. {
  149.     if (ButtonReleased())    {
  150.         int mx, my;
  151.         GetPosition(mx, my);
  152.         DFWindow *Mwnd;
  153.         if ((Mwnd = MouseWindow(mx, my)) == NULL)
  154.             return;
  155.         // ------- disable typematic check
  156.         clickx = clicky = -1;
  157.         delaytimer.DisableTimer();
  158.         // ------- the button was released
  159.         if (mx == releasex && my == releasey)
  160.             // ---- same position as last left button release
  161.             if (doubletimer.TimerRunning())
  162.                 // -- second click before double timeout
  163.                 Mwnd->DoubleClick(mx, my);
  164.         doubletimer.SetTimer(DOUBLETICKS);
  165.         Mwnd->ButtonReleased(mx, my);
  166.         releasex = mx;
  167.         releasey = my;
  168.     }
  169. }
  170.  
  171. void Mouse::DispatchMove()
  172. {
  173.     if (Moved())    {
  174.         int mx, my;
  175.         GetPosition(mx, my);
  176.         DFWindow *Mwnd;
  177.         if ((Mwnd = MouseWindow(mx, my)) != NULL)    {
  178.             Mwnd->MouseMoved(mx, my);
  179.             clickx = clicky = -1;
  180.         }
  181.     }
  182. }
  183.  
  184. void Mouse::DispatchLeftButton()
  185. {
  186.     if (LeftButton())    {
  187.         int mx, my;
  188.         GetPosition(mx, my);
  189.         DFWindow *Mwnd;
  190.         if ((Mwnd = MouseWindow(mx, my)) == NULL)
  191.             return;
  192.         if (mx == clickx && my == clicky)    {
  193.             if (delaytimer.TimedOut())    {
  194.                 // ---- button held down a while
  195.                 delaytimer.SetTimer(DELAYTICKS);
  196.                 // ---- post a typematic-like button
  197.                 Mwnd->LeftButton(mx, my);
  198.             }
  199.         }
  200.         else    {
  201.             // --------- new button press
  202.             delaytimer.SetTimer(FIRSTDELAY);
  203.             if (Mwnd->SetFocus())
  204.                 Mwnd->LeftButton(mx, my);
  205.             clickx = mx;
  206.             clicky = my;
  207.         }
  208.     }
  209. }
  210.  
  211. // -------- dispatch mouse events
  212. void Mouse::DispatchEvent()
  213. {
  214.     DispatchRelease();
  215.     DispatchMove();
  216.     DispatchLeftButton();
  217. }
  218.  
  219.  
  220.  
  221.